package com.neuronbit.businessflow.intellij;

import com.intellij.openapi.Disposable;
import com.j256.simplemagic.ContentInfoUtil;
import org.apache.commons.io.FilenameUtils;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.callback.CefCallback;
import org.cef.handler.*;
import org.cef.misc.BoolRef;
import org.cef.network.CefRequest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Bpm cef request handler.
 */
public class BpmCefRequestHandler extends CefRequestHandlerAdapter {
    private final String myProtocol;
    private final String myAuthority;
    private final Disposable parent;
    private final Map<String, CefResourceHandler> myResources = new HashMap<>();

    private final CefResourceHandler REJECTING_RESOURCE_HANDLER = new CefResourceHandlerAdapter() {
        @Override
        public boolean processRequest(CefRequest request, CefCallback callback) {
            callback.cancel();
            return false;
        }
    };

    private final CefResourceRequestHandler RESOURCE_REQUEST_HANDLER = new CefResourceRequestHandlerAdapter() {
        @Override
        public CefResourceHandler getResourceHandler(CefBrowser browser, CefFrame frame, CefRequest request) {
            try {
                URL url = new URL(request.getURL());
//                if (!url.getProtocol().equals(myProtocol) || !url.getAuthority().equals(myAuthority)) {
//                    return REJECTING_RESOURCE_HANDLER;
//                }

                String re = url.getPath();
                CefResourceHandler handler = myResources.get(re);
                if (handler == null) {
                    String extension = FilenameUtils.getExtension(re);
                    handler = new BpmCefResourceHandler(
                            this.getClass().getClassLoader().getResourceAsStream("editor" + re),
                            ContentInfoUtil.findExtensionMatch(extension).getMimeType(),
                            parent);
                    myResources.put(re, handler);
                }
                return handler;
            } catch (RuntimeException | MalformedURLException e) {
                return REJECTING_RESOURCE_HANDLER;
            }
        }
    };

    /**
     * Instantiates a new Bpm cef request handler.
     *
     * @param myProtocol  the my protocol
     * @param myAuthority the my authority
     * @param parent      the parent
     */
    public BpmCefRequestHandler(String myProtocol, String myAuthority, Disposable parent) {
        this.myProtocol = myProtocol;
        this.myAuthority = myAuthority;
        this.parent = parent;
    }

    /**
     * Add resource.
     *
     * @param resourcePath     the resource path
     * @param resourceProvider the resource provider
     */
    public void addResource(String resourcePath, CefResourceHandler resourceProvider) {
        myResources.put(resourcePath, resourceProvider);
    }

    @Override
    public CefResourceRequestHandler getResourceRequestHandler(CefBrowser browser, CefFrame frame, CefRequest request,
                                                               boolean isNavigation, boolean isDownload,
                                                               String requestInitiator, BoolRef disableDefaultHandling) {
        return RESOURCE_REQUEST_HANDLER;
    }
}
